/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.smarttmt.idu.web.persistence.controller;

import com.smarttmt.idu.ejb.support.EstadoCuentaWrapper;
import com.smarttmt.idu.web.persistence.controller.exceptions.NonexistentEntityException;
import com.smarttmt.idu.web.persistence.controller.exceptions.PreexistingEntityException;
import com.smarttmt.idu.web.persistence.controller.exceptions.RollbackFailureException;
import com.smarttmt.idu.web.persistence.entity.VwEstadocuentaValoriza;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;

/**
 *
 * @author root
 */
public class VwEstadocuentaValorizaJpaController implements Serializable {

    public VwEstadocuentaValorizaJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(VwEstadocuentaValoriza vwEstadocuentaValoriza) throws PreexistingEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            em.persist(vwEstadocuentaValoriza);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findVwEstadocuentaValoriza(vwEstadocuentaValoriza.getProyecto()) != null) {
                throw new PreexistingEntityException("VwEstadocuentaValoriza " + vwEstadocuentaValoriza + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(VwEstadocuentaValoriza vwEstadocuentaValoriza) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            vwEstadocuentaValoriza = em.merge(vwEstadocuentaValoriza);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = vwEstadocuentaValoriza.getProyecto();
                if (findVwEstadocuentaValoriza(id) == null) {
                    throw new NonexistentEntityException("The vwEstadocuentaValoriza with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            VwEstadocuentaValoriza vwEstadocuentaValoriza;
            try {
                vwEstadocuentaValoriza = em.getReference(VwEstadocuentaValoriza.class, id);
                vwEstadocuentaValoriza.getProyecto();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The vwEstadocuentaValoriza with id " + id + " no longer exists.", enfe);
            }
            em.remove(vwEstadocuentaValoriza);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<VwEstadocuentaValoriza> findVwEstadocuentaValorizaEntities() {
        return findVwEstadocuentaValorizaEntities(true, -1, -1);
    }

    public List<VwEstadocuentaValoriza> findVwEstadocuentaValorizaEntities(int maxResults, int firstResult) {
        return findVwEstadocuentaValorizaEntities(false, maxResults, firstResult);
    }

    private List<VwEstadocuentaValoriza> findVwEstadocuentaValorizaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(VwEstadocuentaValoriza.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public VwEstadocuentaValoriza findVwEstadocuentaValoriza(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(VwEstadocuentaValoriza.class, id);
        } finally {
            em.close();
        }
    }

    public java.util.List<VwEstadocuentaValoriza> findVwEstadocuentaValorizaByChip(String chip) {
        EntityManager em = getEntityManager();
        try {
            return em.createNamedQuery("VwEstadocuentaValoriza.findByChip", VwEstadocuentaValoriza.class).setParameter("chip", chip).getResultList();
        } finally {
            em.close();
        }
    }

    public int getVwEstadocuentaValorizaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<VwEstadocuentaValoriza> rt = cq.from(VwEstadocuentaValoriza.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public EstadoCuentaWrapper fromVwEstadocuentaValorizaToEstadoCuentaWrapper(VwEstadocuentaValoriza vwEstadocuentaValoriza) {
        EstadoCuentaWrapper returnObj = new EstadoCuentaWrapper();
        returnObj.setContribu(vwEstadocuentaValoriza.getContribu());
        returnObj.setDescripc(vwEstadocuentaValoriza.getDescripc());
        returnObj.setEstado(vwEstadocuentaValoriza.getEstado());
        returnObj.setFechinic(vwEstadocuentaValoriza.getFechinic());
        returnObj.setProyecto(vwEstadocuentaValoriza.getProyecto());
        returnObj.setSalddeud(vwEstadocuentaValoriza.getSalddeud());
        returnObj.setSaldfavo(vwEstadocuentaValoriza.getSaldfavo());
        return returnObj;
    }
    
    public String validaCHIP(String chip){
        EntityManager em = getEntityManager();
        try {
            String aux="select pkgportal.fun_validachip('";
            String aux2="') from dual";
            return (String) em.createNativeQuery(aux+chip+aux2).getSingleResult();
        } finally {
            em.close();
        }
    }
    
        public String validaPropietarioCHIP(String chip, String tercero){
        EntityManager em = getEntityManager();
        try {
            String aux="select pkgportal.fun_Valida_PropietarioChip('"+tercero+"','"+chip;
            String aux2="') from dual";
            return (String) em.createNativeQuery(aux+aux2).getSingleResult();
        } finally {
            em.close();
        }
    }
    
    public String MatriculaDireccion(String chip){
        EntityManager em = getEntityManager();
        try {
            String aux="select pkgportal.fun_Matricula_Direccion('";
            String aux2="') from dual";
            return (String) em.createNativeQuery(aux+chip+aux2).getSingleResult();
        } finally {
            em.close();
        }
    }

}
